package com.htandroid.htandroid.aty;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.ab.soap.AbSoapListener;
import com.ab.soap.AbSoapParams;
import com.ab.task.AbTask;
import com.ab.task.AbTaskItem;
import com.ab.task.AbTaskObjectListener;
import com.ab.util.AbDialogUtil;
import com.ab.util.AbToastUtil;
import com.htandroid.htandroid.R;
import com.htandroid.htandroid.entity.UffjqgdList;
import com.htandroid.htandroid.sys.InstorageURL;
import com.htandroid.htandroid.sys.User;
import com.htandroid.htandroid.sys.base_index;
import com.htandroid.htandroid.webservice.abXMLJtsplist;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static com.htandroid.htandroid.constance.ConstanceServer.BaseUrl_New;

/**
 * Created by lkp on 2015/11/29.
 */
public class jtsp_index extends base_index {
    private ArrayList<UffjqgdList> qgdlist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.layout_qgdsp_index);
        initview("集团审批");
        initview2();
        getdata(0);
    }

    //初始化
    @Override
    public void initview2() {
//        btn_share.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                String excelName = "0".equals(listtype) ? "待审批" : "已审批";
//                excelPath = getExcelDir() + File.separator + "集团审批" + excelName + ".xls";
//                ExcelPurchase_bmqgsp bmqgsp = new ExcelPurchase_bmqgsp(jtsp_index.this, excelPath, qgdlist, listtype);
//                if (qgdlist.size() == 0) {
//                    AbToastUtil.showToast(getApplicationContext(), "列表中没有数据，不能分享");
//                } else {
//                    OnekeyShare oks = new OnekeyShare();
//                    oks.setTitle("集团审批" + excelName + ".xls");
//                    oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {
//                        @Override
//                        public void onShare(Platform platform,
//                                            Platform.ShareParams paramsToShare) {
//                            // TODO Auto-generated method stub
//                            if (platform.getName().equals("Wechat")) {
//                                paramsToShare.setShareType(Platform.SHARE_FILE);
//                            }
//                        }
//                    });
//                    imgPath = getExcelDir() + File.separator + "excel.png";
//                    oks.setImagePath(imgPath);
//                    oks.setFilePath(excelPath);
//                    oks.show(getApplicationContext());
//                }
//            }
//        });

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                linechoose("r", Integer.valueOf("" + id));
                //从position改为id,因为过滤之后的position不正确
                UffjqgdList jtsp = qgdlist.get((int) id);
                Intent intent = new Intent(getApplicationContext(), jtsp_detail.class);
                //传递参数
                Bundle bundle = new Bundle();
                bundle.putSerializable("uffjqgds", jtsp);
                intent.putExtras(bundle);
                intent.putExtra("listtype", jt_listtype);//传递类型,下一界面便于控制
                startActivityForResult(intent, R.layout.layout_qgdsp_detail / 100000);
            }
        });
    }

    //获取数据
    @Override
    public void getdata(int page) {
        AbTask task = new AbTask();
        final AbTaskItem item = new AbTaskItem();
        item.setListener(new AbTaskObjectListener() {

            @Override
            public <T> void update(T entity) {
                String urlString = "http://" + InstorageURL.SERVERURL + BaseUrl_New;//旧版webservice地址
                AbSoapParams params = new AbSoapParams();
                params.put("arg0", User.uid);
                params.put("arg1", User.nickName);
                params.put("arg2", myApplication.getGs_ztid());
                params.put("arg3", myApplication.getGs_year());
                params.put("arg4", jt_listtype);
                adatperDataList.clear();
                mAbSoapUtil.call(urlString, InstorageURL.NAME_SPACE, "htjt_qgdsp_list", params, new AbSoapListener() {
                    //获取数据成功会调用这里
                    @Override
                    public void onSuccess(int statusCode, String content) {
                        if (content != null) {
                            adatperDataList.clear();
                            ArrayList<Map<String, Object>> dataListForInit = new ArrayList<Map<String, Object>>();
                            qgdlist = abXMLJtsplist.paraseCommentInfors(content);
                            for (int i = 0; i < qgdlist.size(); i++) {
                                //增加一个行选择状态初始化
                                sa2.getIsSelected().put(i, false);
                                UffjqgdList jtsp = new UffjqgdList();
                                jtsp = qgdlist.get(i);
//                                aaa.add(item.getPropertyAsString(0));
                                String sum = jtsp.getSum();
                                if ("".equals(sum)) {
                                    sum = "0.00";
                                }
                                Map<String, Object> map = new HashMap<String, Object>();
                                map.put("index", (i + 1));
                                map.put("czzabbre", "酒店简称：" + jtsp.getCzzabbre());
                                map.put("cvouchid", "单据号：" + jtsp.getCvouchid());
                                map.put("sum", "金额：" + sum);
                                map.put("cdept", "部门：" + jtsp.getCdepname());
                                map.put("cnocheckreason", "请购流程：" + jtsp.getCnocheckreasonname());
                                map.put("ddate", "请购日期：" + jtsp.getDdate().substring(0, 10));
                                //更多数据
                                map.put("data5", "制单人：" + jtsp.getCmaker());
                                map.put("data6", "定价人：" + jtsp.getCprechecker());
                                map.put("data7", "已审步骤：" + jtsp.getCdefine4());
                                String cchar_text = "待审角色：";
                                if (jtsp.getStepall().equals(jtsp.getCdefine4())) {
                                    cchar_text = "已审角色：";
                                }
                                map.put("data8", cchar_text + jtsp.getCchar());
                                dataListForInit.add(map);
                            }
                            toolDataProvider.initData(dataListForInit);
                            loadList();
                        } else {
                            AbToastUtil.showToast(jtsp_index.this, R.string.sqlerrormsg);
                        }
                    }

                    // 失败，调用
                    @Override
                    public void onFailure(int statusCode, String content,
                                          Throwable error) {
                        AbToastUtil.showToast(jtsp_index.this, R.string.sqlerrormsg);
                    }

                    // 开始执行前
                    @Override
                    public void onStart() {
                        //显示进度框
                        AbDialogUtil.showProgressDialog(jtsp_index.this, 0, "正在加载...");
                    }

                    // 完成后调用，失败，成功
                    @Override
                    public void onFinish() {
                        //移除进度框
                        AbDialogUtil.removeDialog(jtsp_index.this);
                    }
                });
            }

            @SuppressWarnings("unchecked")
            @Override
            public Integer getObject() {
                return -1;
            }
        });
        task.execute(item);
    }

    //接收返回数据(判断是否需要刷新当前列表)
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
            case R.layout.layout_qgdsp_detail / 100000:
                String isref = data.getExtras().getString("isref");
                if (isref.equals("1")) {
                    //刷新列表
                    getdata(0);
                    etSearch.getText().clear();
                }
            default:
                //其它窗口的回传数据
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
